/**
 * @file utils 工具方法
 * @author swan
 */
/* global swan */
import {rawPattern} from '@smt-ui/component-gov/src/common/utils/valid-type';
/**
 * 平台信息
 *
 * @public
 */
export const systemInfo = (() => {
    let info = {};
    try {
        info = swan.getSystemInfoSync();
    }
    catch (e) {}
    return info;
})();

/**
 * 检查是不是全面屏
 *
 * @public
 * @return {Function} 是不是全面屏
 */
export const checkFullScreen = (() => {
    return [
        'iPhone X',
        'iPhone XR',
        'iPhone XS',
        'iPhone XS Max',
        'iPhone 11',
        'iPhone 11 Pro',
        'iPhone 11 Pro Max'
    ].some(item => (typeof systemInfo.model === 'string') && systemInfo.model.includes(item));
})();

/**
 * 检查网络状况
 *
 * @public
 * @return {Object} promise实例
 */
export function checkNetwork() {
    return new Promise((resolve, reject) => {
        /* eslint-disable no-undef */
        swan.getNetworkType({
            /* eslint-enable no-undef */
            success(res) {
                if (res && res.networkType === 'none') {
                    reject();
                }
                else {
                    resolve();
                }
            },
            fail(err) {
                reject();
            }
        });
    });
}

/**
 * 身份证号校验
 *
 * @public
 * @param {string} idCard 检验的字符
 * @return {Object} 检验身份证号的error
 */
export const checkIdcard = idCard => {
    if (!idCard) {
        return {
            errFlag: true,
            errContent: '请输入正确的18位身份证号'
        };
    }

    let checkResult = rawPattern.idCard.test(idCard);
    if (!checkResult) {
        return {
            errFlag: true,
            errContent: '请输入正确的18位身份证号'
        };
    }
    return {
        errFlag: false,
        errContent: ''
    };
};

/**
 * 手机号码校验
 *
 * @public
 * @param {string} phoneCard 检验的字符
 * @return {Object} 检验手机号的error
 */
export const checkPhone = phoneCard => {
    if (!phoneCard) {
        return {
            errFlag: true,
            errContent: '请输入正确的11位手机号'
        };
    }

    let checkResult = rawPattern.mobile.test(phoneCard);
    if (!checkResult) {
        return {
            errFlag: true,
            errContent: '请输入11位电话号码'
        };
    }
    return {
        errFlag: false,
        errContent: ''
    };
};

/**
 * 空校验
 *
 * @public
 * @param {string} value 检验的字符
 * @param {string} promptText 提示文本
 * @return {Object} 检验空的error
 */
export const checkEmpty = (value, promptText) => {
    let validEmpty = rawPattern.noEmpty.test(value);
    if (!validEmpty) {
        return {
            errFlag: true,
            errContent: promptText
        };
    }

    return {
        errFlag: false,
        errContent: ''
    };
};
